package com.sinodata.bsm.cicp.collector.net.trap;

import java.util.Map;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.PDU;
import org.snmp4j.PDUv1;
import org.snmp4j.smi.VariableBinding;

import com.sinodata.bsm.cicp.collector.net.trap.impl.TrapProcess;
import com.sinodata.bsm.common.utils.DateUtil;

public class TCPProcess extends TrapProcess {
    Logger logger = Logger.getLogger(TCPProcess.class.getName());
    //    private TrapProcess nextTrapProcess;

    private Map<String, String[]> map = TCPTranslate.map;
    private GetTPCResId getTPCResId = new GetTPCResId();
    private TCPMsgTemplate tpcMsgTemplate = new TCPMsgTemplate();
    private EventSent eventSent = new EventSent();

    public boolean trapProcess(CommandResponderEvent respEvnt) {
        boolean flag = false;
        PDU p = respEvnt.getPDU();
        if (p instanceof PDUv1) {
            PDUv1 p1 = (PDUv1) p;
            String oidString = p1.getEnterprise().toString();
            if (oidString.equals("1.3.6.1.4.1.2.6.194.4.1") || oidString.equals("1.3.6.1.4.1.2.6.194.2.1")) {
                //                List eventOidList = new ArrayList();
                //                int genericTrapType = p1.getGenericTrap();
                int specificTrapType = p1.getSpecificTrap();
                long time = p1.getTimestamp();

                String eventOid = oidString + ".0." + specificTrapType;
                String eventDesc = map.get(eventOid)[0];
                String eventlevel = map.get(eventOid)[1];

                Document document = DocumentHelper.createDocument();
                document.setXMLEncoding("gbk");
                Element root = document.addElement("request");
                Element eventElement = root.addElement("event");

                //                String ipString = p1.getAgentAddress().toString(); // 测试是否包含端口
                // String resId = getTPCResId.getTPCResId(ipString);

                StringBuffer eventMsg = new StringBuffer();
                eventMsg.append(eventDesc).append(": ");

                Vector<VariableBinding> recVBs = p1.getVariableBindings();
                String resId = "";
                for (int i = 0; i < recVBs.size(); i++) {
                    VariableBinding recVB = recVBs.elementAt(i);
                    String oid = recVB.getOid().toString();
                    String value = recVB.getVariable().toString();

                    if (oid.equals("1.3.6.1.4.1.2.6.194.2.1.1") || oid.equals("1.3.6.1.4.1.2.6.194.4.1.3")) {
                        resId = getTPCResId.getTPCResId(value);
                    }

                    if (i == recVBs.size() - 1) {
                        eventMsg.append(tpcMsgTemplate.getMsg(oid, value));
                    } else {
                        eventMsg.append(tpcMsgTemplate.getMsg(oid, value)).append(",");
                    }
                }
                String timeString = DateUtil.getDateTime(time); // 转换为
                // yyyy-mm-dd
                // hh:mm:ss格式的时间
                eventElement.addElement("res").addText(resId);
                eventElement.addElement("type").addText("700000"); // 定义好eventType之后写死在程序中
                eventElement.addElement("level").addText(eventlevel);
                eventElement.addElement("time").addText(timeString);
                eventElement.addElement("fault").addText("");
                eventElement.addElement("msg").addText(eventMsg.toString());
                String str = document.asXML();
                str = str.replaceAll("\n", "");
                eventSent.sendEvent(str);
            }
            flag = true;
        }
        return flag;
    }

}
